package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.database.DataBase;
import cn.sccl.common.database.DataRow;
import cn.sccl.common.database.DataSet;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.util.StrUtils;
import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.Element;
import cn.sccl.dirms.monitor.model.Link;
import cn.sccl.dirms.monitor.model.SysmapLink;
import cn.sccl.dirms.monitor.model.Sysmaps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by PCRMSAPP on 2017/4/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/sysmap")
public class SysMapController {
    @Autowired
    private DataBase dataBase;
    /**
     * 更新元素的坐标xy
     * @param elementList 拓扑图元素节点列表
     * @return
     */
    @RequestMapping(value = "/update/element/x/y", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Element> updateElementXy(@RequestBody List<Element> elementList) {
        StringBuffer asf = new StringBuffer();
        for (Element element : elementList) {
            StringBuffer sf = new StringBuffer();
            sf.append("(");
            sf.append(element.getSysmapid()+",");
            sf.append(element.getSelementid()+",");
            sf.append(element.getX()+",");
            sf.append(element.getY()+")");
            asf.append(sf).append(",");
        }
        String sqlVal = asf.toString();
        if (sqlVal.isEmpty()) {
            return null;
        }
        sqlVal = sqlVal.substring(0, sqlVal.length()-1);


        String sql = "insert into sysmaps_elements(sysmapid, selementid, x, y) \n" +
                "values";
        sql += sqlVal + " on duplicate key update x=values(x),y=values(y)";
        // System.out.println(sql);

        int ret = this.dataBase.executeUpdate(sql);
        if (ret <= 0) {
            return null;
        }
        return elementList;
    }
    /**
     * 自动生成拓扑图元素数据。
     * @param hostid 监控项id
     */
    @RequestMapping(value = "/autogenmap/{hostid}/{mapid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> autogenmap(@PathVariable String hostid,@PathVariable  String mapid) {
        Zabbix zabbix = Zabbix.getZabbix();
        CommonController commonController = new CommonController();
       //根据交换机id,取得监控项，创建不存在的主机
        //取得主机的应用集
        Map<String,Object> applicationparams = new HashMap<String,Object>();
        applicationparams.put("output", "extend");
        applicationparams.put("hostids", hostid);
        applicationparams.put("sortfield","name");
        applicationparams.put("selectItems","extend");
        Map<String, Object> applicatonresult = zabbix.call("application.get", applicationparams);
        List<Map<String, Object>> applicationmapList = (List<Map<String, Object>>) applicatonresult.get("result");
        List<Map<String, Object>> vlanitemmmapList = new ArrayList<>();
        List<Map<String, Object>> ipitemmapList = new ArrayList<>();

        //根据监控项进行遍历
        for(Map map : applicationmapList){
            if(map.get("name").equals("VLAN")){
                if(null != map.get("items")){
                    vlanitemmmapList =  (List<Map<String, Object>>)map.get("items");
                }
            }
            if(map.get("name").equals("IP")){
                ipitemmapList = (List<Map<String, Object>>)map.get("items");
            }
        }
        Map<String,String>  mapHosts= new HashMap<>();
        //创建建控主机,
        //首先取得ip地址字符串，进行第一步的截取
        //进行分组
        Map<String,Map<String,String>>  mapsvlanGroup = new HashMap<>();
        Map<String,Map<String,String>>  mapshubGroup = new HashMap<>(); //hub 交换器的分组
        for(Map map  : ipitemmapList){
            if(null != map.get("name")){
                String ipstr =   map.get("name").toString();
                String  tmpipstr =  ipstr.substring(ipstr.indexOf(".")+1,ipstr.length());
                String  vlanStr= ipstr.substring(0,ipstr.indexOf("."));
                String tmphostid="";
                //判断主机是否已经存在
                if (!commonController.hasExistHost(tmpipstr)){
                    //创建主机
                   tmphostid = commonController.createHost(ipstr,commonController.queryGroupbyName("Discovered hosts"));
                   mapHosts.put(tmpipstr,tmphostid);
                }else{
                    tmphostid =commonController.queryforHostByIP(tmpipstr);
                    mapHosts.put(tmpipstr,tmphostid);
                }
                if(tmpipstr.substring(tmpipstr.lastIndexOf(".")+1,tmpipstr.length()).equals("254")){//交换机分组
                    Map<String,String> mapshub = new HashMap<>();
                    mapshub = mapshubGroup.get(vlanStr);
                    if(null == mapshub){
                        mapshub = new HashMap<>();
                    }
                    mapshub.put(tmphostid,tmpipstr);
                    mapshubGroup.put(vlanStr,mapshub);
                }else {
                   Map<String,String>  mapsthirdhost = new HashMap<>();
                    mapsthirdhost = mapsvlanGroup.get(vlanStr);
                    if(null == mapsthirdhost){
                        mapsthirdhost = new HashMap<>();
                    }
                    mapsthirdhost.put(tmphostid,tmpipstr);
                    mapsvlanGroup.put(vlanStr,mapsthirdhost);
                }
            }
        }


        Map<String,String>  elementlist = new HashMap<>();
         if (mapshubGroup.size() >0){ //进行三层交换机处理

             List<Map<String,String>>  list =  new ArrayList<>();
             commonController.createElementsAndLinksList(hostid,mapshubGroup,mapsvlanGroup,mapid);


         }else  if(mapsvlanGroup.size() >0){ // 只有两层 交换机的处理
             List<Map<String,String>>  list =  new ArrayList<>();
             list.add(commonController.genElementMap(hostid));
            // list = commonController.createElementList(mapsvlanGroup);
             //commonController.createElement(list,mapid);
        }

        // 根据分组，来增加拓扑元素和线
      //  Map<String,Map<String,String>>  mapsvlanGroup = new HashMap<>();

        //创建topo图元素
        /*
        for(Map map : vlanitemmmapList){
            if(null != map.get("name")){
                String vlanvalue = map.get("name").toString();
                
            }
        }
        */

        //
        /*
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectSelements", "extend");
        params.put("selectLinks", "extend");
        params.put("selectUser","enxtnd");
        params.put("selectUserGroups","extend");
        params.put("sysmapids", mapid);
        Map<String, Object> result = zabbix.call("map.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        */
        List<Map<String, Object>> mapList = new ArrayList<>();
        return mapList;

    }

    /**
     * 获取所有IP
     * @param hostid 核心交换机种子ip
     */
    @RequestMapping(value = "/getIPs/{hostid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List getIPs(@PathVariable String hostid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> applicationparams = new HashMap<String, Object>();
        applicationparams.put("output", "extend");
        applicationparams.put("hostids", hostid);
        applicationparams.put("sortfield", "name");
        applicationparams.put("selectItems", "extend");
        Map<String, String> filter = new HashMap<>();
        filter.put("name", "IP");
        applicationparams.put("filter", filter);
        Map<String, Object> applicatonresult = zabbix.call("application.get", applicationparams);
        List<Map<String, Object>> applicationmapList = (List<Map<String, Object>>) applicatonresult.get("result");
        List<Map<String, Object>> ipitemmapList = new ArrayList<>();
        if (applicationmapList.size() == 1) {
            ipitemmapList = (List<Map<String, Object>>)applicationmapList.get(0).get("items");
        }
        return ipitemmapList;
    }

    /**
     * 获取所有VLAN和ip，以及对应关系
     * @param hostid 核心交换机种子ip
     */
    @RequestMapping(value = "/vlan/ip/{hostid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Map<String,List<String>>> getVlanAndIP(@PathVariable String hostid) {
        Zabbix zabbix = Zabbix.getZabbix();
        CommonController commonController = new CommonController();
        //根据交换机id,取得监控项，创建不存在的主机
        //取得主机的应用集
        Map<String,Object> applicationparams = new HashMap<String,Object>();
        applicationparams.put("output", "extend");
        applicationparams.put("hostids", hostid);
        applicationparams.put("sortfield","name");
        applicationparams.put("selectItems","extend");
        Map<String, String> filter = new HashMap<>();
        filter.put("name", "IP");
        applicationparams.put("filter", filter);
        Map<String, Object> applicatonresult = zabbix.call("application.get", applicationparams);
        List<Map<String, Object>> applicationmapList = (List<Map<String, Object>>) applicatonresult.get("result");

        List<Map<String, Object>> ipitemmapList = new ArrayList<>();
        if (applicationmapList.size() == 1) {
            ipitemmapList = (List<Map<String, Object>>)applicationmapList.get(0).get("items");
        }

        Map<String,String>  mapHosts= new HashMap<>();
        //创建建控主机,
        //首先取得ip地址字符串，进行第一步的截取
        //进行分组
        Map<String, Map<String,List<String>>>  mapshubGroup = new HashMap<>(); //hub 交换器的分组
        for(Map map  : ipitemmapList){
            if(null != map.get("name")){
                String ipstr = map.get("name").toString();
                String tmpipstr = ipstr.substring(ipstr.indexOf(".")+1,ipstr.length());
                String vlanStr = ipstr.substring(0, ipstr.indexOf("."));

                Map<String, List<String>> mapshub = mapshubGroup.get(vlanStr);
                if(null == mapshub){
                    mapshub = new HashMap<>();
                    mapshubGroup.put(vlanStr, mapshub);
                }

                if(tmpipstr.substring(tmpipstr.lastIndexOf(".")+1,tmpipstr.length()).equals("254")){//交换机分组
                    List<String> listip = mapshub.get(tmpipstr);
                    if (null == listip) {
                        listip = new ArrayList<>();
                        mapshub.put(tmpipstr, listip);
                    }
                }else {
                    String hubip = tmpipstr.substring(0, tmpipstr.lastIndexOf(".")+1) + "254";
                    List<String> listip = mapshub.get(hubip);
                    if (null == listip) {
                        listip = new ArrayList<>();
                        listip.add(tmpipstr);
                        mapshub.put(hubip, listip);
                    } else {
                        listip.add(tmpipstr);
                    }
                }
            }
        }
        return mapshubGroup;
    }

    /**
     * 自动创建拓扑图
     * @param interfaceData 交换机数据
     * @param sysmapid 拓扑图id
     */
    @RequestMapping(value = "/autoCreateSysmap/{sysmapid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> autoCreateSysmap(
            @PathVariable String sysmapid,
            @RequestBody Map<String, Object> interfaceData) {

        Map resultMap = new HashMap<>();

        CommonController commonController = new CommonController();
        Map<String, Object> hostList = commonController.listHost();

        Iterator iter = interfaceData.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();

            // hub
            String hubip = (String) entry.getKey();
            if (hubip.equals("_")) {
                continue;
            }

            String hubHostId = commonController.getHostId(hubip, hostList);
            if (null == hubHostId) {
                hubHostId = commonController.createHost(hubip, commonController.queryGroupbyName("Discovered hosts"));
            }

            // ip list
            List<String> ipList = (List<String>) entry.getValue();
            List ipmapList = new ArrayList<>();
            for (String ipaddr: ipList) {
                String ipHostId = commonController.getHostId(ipaddr, hostList);
                if (null == ipHostId) {
                    ipHostId = commonController.createHost(ipaddr, commonController.queryGroupbyName("Discovered hosts"));
                }

                Map<String, String> ipMap = new HashMap<>();
                ipMap.put(ipHostId, ipaddr);
                ipmapList.add(ipMap);
            }

            Map<String, List> allIpMap = new HashMap<>();
            allIpMap.put(hubip, ipmapList);
            resultMap.put(hubHostId, allIpMap);
        }
        return resultMap;
    }

    /**
     * 获取所有拓扑图基础数据。
     */
    @RequestMapping(value = "/all", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> listAll() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        Map<String, Object> result = zabbix.call("map.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     * 获取某种类型的拓扑图基础数据
     * @param type 拓扑图类型
     */
    @RequestMapping(value = "/getSysmapByType/{type}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Sysmaps> getSysmapByType(@PathVariable String type) {
        String sql = "select s.sysmapid, s.name, s.bus_type from sysmaps s ";
        List params = new ArrayList();
        if (!type.isEmpty()) {
            sql += "where 1=1 and s.bus_type = ?";
            params.add(type);
        }
        return this.dataBase.query4Model(sql, Sysmaps.class, params.toArray());
    }

    /**
     * 根据名称查找拓扑图基础数据
     */
    @RequestMapping(value = "/getSysmapByName", method = {RequestMethod.POST,RequestMethod.GET})
    public Sysmaps getSysmapByName(@RequestBody Sysmaps sysmap) {
        String sql = "select s.sysmapid, s.name, s.bus_type from sysmaps s ";
        List params = new ArrayList();
        if (!sysmap.getName().isEmpty()) {
            sql += "where 1=1 and s.name = ?";
            params.add(sysmap.getName());
        }
        List<Sysmaps> sysmapsList = this.dataBase.query4Model(sql, Sysmaps.class, params.toArray());
        if (sysmapsList.size() == 1) {
            return sysmapsList.get(0);
        }
        return null;
    }

    /**
     * 根据ID，获取所有拓扑图元素数据。
     * @param id 拓扑图id
     */
    @RequestMapping(value = "/get/{id}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> getMapById(@PathVariable String id) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("selectSelements", "extend");
        params.put("selectLinks", "extend");
        params.put("selectIconMap", "extend");
//        params.put("selectUrls", "extend");
//        params.put("selectUsers","enxtnd");
//        params.put("selectUserGroups","extend");
//        params.put("expandUrls", true);
        params.put("sysmapids", id);
        Map<String, Object> result = zabbix.call("map.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     * 更新拓扑图元素
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST,RequestMethod.GET})
    public boolean updateMap(@RequestBody List<Element> elementList) {
        for (Element element: elementList) {
            String sql = "update sysmaps_elements s set ";
            boolean flag = false;
            List params = new ArrayList();
            if (!element.getLabel().isEmpty()) {
                sql += "s.label=?";
                params.add(element.getLabel());
                flag = true;
            }
            if (!element.getIconid_off().equals("0")) {
                sql += ", s.iconid_off=?";
                params.add(element.getIconid_off());
                flag = true;
            }
            if (!element.getIconid_on().equals("0")) {
                sql += ", s.iconid_on=?";
                params.add(element.getIconid_on());
                flag = true;
            }
            if (!element.getIconid_maintenance().equals("0")) {
                sql += ", s.iconid_maintenance=?";
                params.add(element.getIconid_maintenance());
                flag = true;
            }
            if (!element.getIconid_disabled().equals("0")) {
                sql += ", s.iconid_disabled=?";
                params.add(element.getIconid_disabled());
                flag = true;
            }
            sql += " where s.selementid=? and s.sysmapid=?";
            params.add(element.getSelementid());
            params.add(element.getSysmapid());
            if (flag) {
                int ret = this.dataBase.executeUpdate(sql, params.toArray());
                if (ret <= 0) {
                    return false;
                }
            }
        }
        return true;
    }


    /**
     * 根据拓扑图id和节点id获取连接数据
     * @param mapid 拓扑图id
     * @param nodeid 节点id
     */
    @RequestMapping(value = "/getLink/{mapid}/{nodeid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<SysmapLink> getLinksByNodeid(@PathVariable String mapid, @PathVariable String nodeid) {
        String sql = "select s.linkid, s.sysmapid, s.selementid1, s.selementid2, s.drawtype, s.color, s.label " +
                "from sysmaps_links s where 1=1 ";
        List params = new ArrayList();
        if (!mapid.isEmpty()) {
            sql += "and s.sysmapid = ? ";
            params.add(mapid);
        }
        if (!nodeid.isEmpty()) {
            sql += "and (s.selementid1 = ? or s.selementid2 = ?)";
            params.add(nodeid);
            params.add(nodeid);
        }
        List<SysmapLink> s = this.dataBase.query4Model(sql, SysmapLink.class, params.toArray());
        return this.dataBase.query4Model(sql, SysmapLink.class, params.toArray());
    }

    /**
     * 分页获取获取所有拓扑图基础数据。
     */
    @RequestMapping(value = "/pagequery/{pageNo}/{pageSize}", method = {RequestMethod.POST,RequestMethod.GET})
    public PageResult<Sysmaps> pageQuerySysMap(@PathVariable long pageNo, @PathVariable  int pageSize) {
        String sql = "select s.sysmapid, s.name, s.bus_type from sysmaps s";
        return this.dataBase.query4Model(sql, pageNo, pageSize, Sysmaps.class);
    }

    /**
     * 更新拓扑图基础数据。
     */
    @RequestMapping(value = "/updateSysmap", method = {RequestMethod.POST,RequestMethod.GET})
    public boolean updateSysMap(@RequestBody Sysmaps sysmaps) {
        String sql = "update sysmaps s set ";
        boolean flag = false;
        List params = new ArrayList();
        if (!sysmaps.getName().isEmpty()) {
            sql += "s.name=?, ";
            params.add(sysmaps.getName());
            flag = true;
        }
        if (!sysmaps.getBusType().isEmpty()) {
            sql += "s.bus_type=? ";
            params.add(sysmaps.getBusType());
            flag = true;
        }
        if (!sysmaps.getSysmapid().isEmpty()) {
            sql += "where 1=1 and s.sysmapid=?";
            params.add(sysmaps.getSysmapid());
        }
        if (flag) {
            int ret = this.dataBase.executeUpdate(sql, params.toArray());
            if (ret <= 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 创建空的拓扑图
     * @param sysmaps 拓扑图对象
     */
    @RequestMapping(value = "/create", method = {RequestMethod.POST,RequestMethod.GET})
    public Sysmaps createSysmap(@RequestBody Sysmaps sysmaps) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("name", sysmaps.getName());
        params.put("width", 600);
        params.put("height", 600);
        Map<String, Object> result = zabbix.call("map.create", params);
        Map<String, Object> mapResult = (Map<String, Object>) result.get("result");

        List sysmapids = (List) mapResult.get("sysmapids");
        if (sysmapids.size() == 1) {
            String sysmapid = (String) sysmapids.get(0);
            sysmaps.setSysmapid(sysmapid);

            boolean ret = this.updateSysMap(sysmaps);
            if (ret) {
                return sysmaps;
            }
        }
        return null;
    }

    /**
     * 删除拓扑图
     * @param sysmapid 拓扑图id
     */
    @RequestMapping(value = "/delete/{sysmapid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> deleteSysmap(@PathVariable String sysmapid) {
        Zabbix zabbix = Zabbix.getZabbix();
        List params = new ArrayList();
        params.add(sysmapid);
        Map<String, Object> result = zabbix.call("map.delete", params);
        Map<String, Object> mapResult = (Map<String, Object>) result.get("result");
        return mapResult;
    }

    /**
     * 删除拓扑图
     * @param sysmapids 拓扑图id列表
     */
    @RequestMapping(value = "/deleteTopos", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> deleteSysmap(@RequestBody List<String> sysmapids) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> result = zabbix.call("map.delete", sysmapids);
        Map<String, Object> mapResult = (Map<String, Object>) result.get("result");
        return mapResult;
    }

    /**
     * 添加拓扑图元素
     * @param elementList 元素列表
     */
    @RequestMapping(value = "/add/element", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Element> addMapElement(@RequestBody List<Element> elementList) {
        StringBuffer asf = new StringBuffer();
        for (Element element : elementList) {
            String uuid = StrUtils.generateShortUuid();
            StringBuffer sf = new StringBuffer();
            sf.append("(");
            sf.append(uuid+",");
            sf.append(element.getSysmapid()+",");
            sf.append(element.getElementid()+",");
            sf.append(element.getElementtype()+",");
            sf.append(element.getIconid_off()+",");
            sf.append("'"+element.getLabel()+"',");
            sf.append("-1, 1, 1, '0', '0', '200', '200', '0', '1')");
            asf.append(sf).append(",");
            element.setSelementid(uuid);
        }
        String sqlVal = asf.toString();
        if (sqlVal.isEmpty()) {
            return null;
        }
        sqlVal = sqlVal.substring(0, sqlVal.length()-1);


        String sql = "insert into sysmaps_elements(selementid, sysmapid, elementid, elementtype, \n" +
                "iconid_off, label, label_location, x, y, elementsubtype, areatype, \n" +
                "width, height, viewtype, use_iconmap) \n" +
                "values";
        sql += sqlVal;
        System.out.println(sql);

        int ret = this.dataBase.executeUpdate(sql);
        if (ret <= 0) {
            return null;
        }

        return elementList;
    }

    /**
     * 添加拓扑图链接
     * @param link 链接元素
     */
    @RequestMapping(value = "/add/link", method = {RequestMethod.POST,RequestMethod.GET})
    public Link addMapLink(@RequestBody Link link) {
        String uuid = StrUtils.generateShortUuid();
        String sql = "insert into sysmaps_links(linkid, sysmapid, selementid1, selementid2, \n" +
                "drawtype, color, label) \n" +
                "values(?, ?, ?, ?, ?, ?, ?)";
        List params = new ArrayList();
        params.add(uuid);
        params.add(link.getSysmapid());
        params.add(link.getSelementid1());
        params.add(link.getSelementid2());
        params.add(link.getDrawtype());
        params.add(link.getColor());
        params.add(link.getLabel());

        int ret = this.dataBase.executeUpdate(sql, params.toArray());
        if (ret <= 0) {
            return null;
        }
        link.setLinkid(uuid);
        return link;
    }

    /**
     * 添加拓扑图链接
     * @param links 链接元素列表
     */
    @RequestMapping(value = "/add/links", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Link> addMapLinks(@RequestBody List<Link> links) {
        StringBuffer asf = new StringBuffer();
        for (Link link: links) {
            String uuid = StrUtils.generateShortUuid();
            StringBuffer sf = new StringBuffer();
            sf.append("(");
            sf.append(uuid+",");
            sf.append(link.getSysmapid()+",");
            sf.append(link.getSelementid1()+",");
            sf.append(link.getSelementid2()+",");
            sf.append(link.getDrawtype()+",");
            sf.append("'"+link.getColor()+"',");
            sf.append("'"+link.getLabel()+"')");

            asf.append(sf).append(",");
            link.setLinkid(uuid);
        }
        String sqlVal = asf.toString();
        if (sqlVal.isEmpty()) {
            return null;
        }
        sqlVal = sqlVal.substring(0, sqlVal.length()-1);

        String sql = "insert into sysmaps_links(linkid, sysmapid, selementid1, selementid2, \n" +
                "drawtype, color, label) \n" +
                "values";
        sql += sqlVal;
//        System.out.println(sql);

        int ret = this.dataBase.executeUpdate(sql);
        if (ret <= 0) {
            return null;
        }
        return links;
    }

    /**
     * 删除拓扑图元素
     * @param selementid 元素id
     */
    @RequestMapping(value = "/delete/element/{selementid}", method = {RequestMethod.POST,RequestMethod.GET})
    public String deleteMapElement(@PathVariable String selementid) {
        String sql = "delete from sysmaps_elements where selementid=?";
        int ret = this.dataBase.executeUpdate(sql, new Object[]{selementid});
        if (ret <= 0) {
            return null;
        }
        return selementid;
    }

    /**
     * 删除拓扑图链接
     * @param linkid 链接元素id
     */
    @RequestMapping(value = "/delete/link/{linkid}", method = {RequestMethod.POST,RequestMethod.GET})
    public String deleteMapLink(@PathVariable String linkid) {
        String sql = "delete from sysmaps_links where linkid=?";
        int ret = this.dataBase.executeUpdate(sql, new Object[]{linkid});
        if (ret <= 0) {
            return null;
        }
        return linkid;
    }


    /**
     * 获取hostid(elementid)和出口流量入口流量的关系
     */
    @RequestMapping(value = "/getHostIdToIncomingAndOutgoingData", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> getHostIdToIncomingAndOutgoingData() {
        CommonController commonController = new CommonController();
        return commonController.getHostIdToIncomingAndOutgoingData();
    }
}
